<?php
	global $page, $do, $act, $tpl, $db, $lg, $title_page, $keywords, $descriptions, 
	$img_og, $link_og, $main_title, $main_keyword, $main_descriptions;
	$sql = "SELECT * FROM `constant` WHERE `alias_name` = 'FB' and `active` = '1'";
    $fb = $db->getAll($sql);
	
	$title_page = $fb[0]['value_vn'];
	$link_og = $fb[1]['value_vn'];
	$descriptions = $fb[2]['value_vn'];
	$img_og = $fb[3]['value_vn'];
	
	$sql = "SELECT * FROM `constant` WHERE `alias_name` = 'main' and `active` = '1'";
    $main_page = $db->getAll($sql);
	
	$main_title = $main_page[0]['value_vn'];
	$main_keyword = $main_page[1]['value_vn'];
	$main_descriptions = $main_page[2]['value_vn'];
	
		// pid = 111 : main/root
		if(isset($_GET['do'])){
			//echo 'co do';
			$do   = CleanSQLInjection(isset($_GET["do"])  ? $_GET["do"] :'main');
			$act   = CleanSQLInjection(isset($_GET["act"])  ? $_GET["act"]  : "main");
			
			$sql = "select * from categories where unique_key_$lg='index' and pid=111";
			$cat1 = $db->getRow($sql);
					
			//seo
			$title_page = $cat1["title_$lg"];
			$keywords = $cat1["keyword_$lg"];
			$descriptions = $cat1["des_$lg"];
		}
		else{
			//echo 'khong co do';
			
			if(isset($_GET['cat1'])){
				$cat1_key = CleanSQLInjection($_GET['cat1']);
				//echo '<br/>[cat1_key:'.$cat1_key.']';
				if($cat1_key == "index"){
					$do = "main";
					$act = "main";
					$sql = "select * from categories where unique_key_$lg='$cat1_key' and pid=111";
					$cat1 = $db->getRow($sql);
					//echo 'test cat1_key == index';
					//seo
					$title_page = isset($cat1["title_$lg"])?$cat1["title_$lg"]:$main_title;				
					$keywords = isset($cat1["keyword_$lg"])?$cat1["keyword_$lg"]:$main_keyword;				
					$descriptions = isset($cat1["des_$lg"])?$cat1["des_$lg"]:$main_descriptions;
				}
				else{
					$sql = "select * from categories where unique_key_$lg='$cat1_key' and pid=111";
					//echo $sql;
					$cat1 = $db->getRow($sql);
					//echo '<br/>'.'[cat1_key != index:'.print_r($cat1).']';
					//seo
					$title_page = $cat1["title_$lg"];
					$keywords = $cat1["keyword_$lg"];
					$descriptions = $cat1["des_$lg"];
					
					if($cat1['has_child'] == 1){
						$do = "submenu";
						$act = "list";
						//echo '<br/>co con<br/>';
					}
					else{
						$sql = "select do, act from component where id=".$cat1['comp'];
						//echo $sql;
						$r = $db->getRow($sql);
						$do = $r['do'];
						$act = $r['act'];
						//echo '<br/>ko co con 2<br/>';
						//echo $do."-".$act;
					}
					if(isset($_GET['cat2'])){
						global $cat2;
						$cat2_key = CleanSQLInjection($_GET['cat2']);
						$sql = "select * from categories where unique_key_$lg='$cat2_key' and pid=" . $cat1['id'];
						
						$cat2 = $db->getRow($sql);
						//echo '[cat2:';print_r($cat2);echo ']';
	
						//seo
						$title_page = $cat2["title_$lg"];
						$keywords = $cat2["keyword_$lg"];
						$descriptions = $cat2["des_$lg"];
					
						$sql = "select do, act from component where id=".$cat2['comp'];
						$r = $db->getRow($sql);
						$do = $r['do'];
						$act = $r['act'];
						
						if($cat2['has_child'] == 1){
						$do = "submenu";
						$act = "list";
						}
						else{
							$sql = "select do, act from component where id=".$cat2['comp'];
							$r = $db->getRow($sql);
							$do = $r['do'];
							$act = $r['act'];
						}
						
						if(isset($_GET['cat3'])){
							global $cat3;
							$cat3_key = CleanSQLInjection($_GET['cat3']);
							$sql = "select * from categories where unique_key_$lg='$cat3_key' and pid=" . $cat2['id'];
							$cat3 = $db->getRow($sql);
		
							//seo
							$title_page = $cat3["title_$lg"];
							$keywords = $cat3["keyword_$lg"];
							$descriptions = $cat3["des_$lg"];
						
							$sql = "select do, act from component where id=".$cat3['comp'];
							$r = $db->getRow($sql);
							$do = $r['do'];
							$act = $r['act'];
							
						}
					}
					if(isset($_GET['unique_key'])){
						$act = "detail";
						//echo 'test';
					}
				}
			}
			else{
				$sql = "select * from categories where unique_key_$lg='index' and pid=111";
				$cat1 = $db->getRow($sql);
				//echo $sql;
				//echo '<br/>khong co do + khong co cat1';
				//seo
				
				$title_page = isset($cat1["title_$lg"])?$cat1["title_$lg"]:$main_title;				
				$keywords = isset($cat1["keyword_$lg"])?$cat1["keyword_$lg"]:$main_keyword;				
				$descriptions = isset($cat1["des_$lg"])?$cat1["des_$lg"]:$main_descriptions;
				
				$do = "main";
				$act = "main";
			}
			
		}
		//echo '<br/>[do:'.$do.']' . ' [act:'.$act.']'.'[cat1:'.$_GET['cat1'].']'.'[cat2:'.$_GET['cat2'].']'.'[unique_key:'.$_GET['unique_key'].']';
		$page = isset($_GET["page"])?is_numeric($_GET["page"])?$_GET["page"]:'1':'1';
		
		$file_index = "./templates/templates.php";
		if (file_exists($file_index)){
			require($file_index);
		}
		else{
			//$msg = REDIRECT_MESSAGE;
			$p = '/index.php';
		}	
?>